package cn.wolfcode.service.impl;

import cn.wolfcode.domain.OperateIntergralVo;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.mapper.OrderInfoMapper;
import cn.wolfcode.service.IOrderInfoService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


//@Service
public class OrderInfoServiceImpl_bak implements IOrderInfoService {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Override
    @Transactional
    public String refund(String orderNo) {
        // 查询订单数据
        OrderInfo orderInfo = orderInfoMapper.select(orderNo);
        // 将订单数据封装到消息中
        OperateIntergralVo vo = new OperateIntergralVo();
        vo.setPk(orderInfo.getOrderNo());
        vo.setUserId(orderInfo.getUserId());
        vo.setValue(orderInfo.getIntergral());
        vo.setInfo("退款操作");
        Message<OperateIntergralVo> message = MessageBuilder.withPayload(vo).build();
        // 发送消息
        rocketMQTemplate.syncSend("tx_topic",message);


//        int i = 1 / 0;  // 发送这个普通syncSend消息还是会存在分布式事务问题,需要发送一个事务消息


        // 修改订单状态
        orderInfoMapper.changeRefundStatus(orderNo,OrderInfo.STATUS_REFUND);
        return "退款成功";
    }

    @Override
    public void executeLocalTransaction(String orderNo) {

    }
}
